<?php

class MallBrandsController extends Controller
{
    /*
    * 获取商品品牌列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"name","name");


            //获取count
            $totalcount = MallBrands::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = MallBrands::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }



    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $introduction = Util::getParamNotEmpty("introduction");


            //添加
            $obj = new MallBrands();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->pic_url = $pic_url;
            $obj->introduction = $introduction;



            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $introduction = Util::getParamNotEmpty("introduction");


            //验证存在
            $obj = MallBrands::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->name = $name;
                $obj->pic_url = $pic_url;
                $obj->introduction = $introduction;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallBrands::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取二级菜单列表对应的品牌
    *
    */
    public function actionGetBrandsListPaging()
    {
        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数选择
            $material_id = Util::getParamNotEmpty("material_id");


            //获取品牌
            $returnlist = array();
            $searchsql = Util::initSearchSql();
            $list =  MallBrandsMaterial::model()->findAll($searchsql." and material_id=".$material_id." order by material_id asc ");
            foreach($list as $key => $value ){
                $blist = MallBrands::model()->findbyPK($value->brands_id);
                array_push($returnlist,$blist);
            }


            //返回数据
            Util::responseList(count($returnlist), $returnlist);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());


        }
    }

    /*
    * 添加
    *
    */
    public function actionAddBrands()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $material_id = Util::getParamNotEmpty("material_id");
            $brands_id = Util::getParamNotEmpty("brands_id");
            $type_id= Util::getParamNotEmpty("type_id");


            //添加
            $obj = new MallBrandsMaterial();
            Util::initObjData($obj);


            $obj->material_id = $material_id;
            $obj->brands_id = $brands_id;
            $obj->type_id = $type_id;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDeleteBrands()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $material_id = Util::getParamNotEmpty("material_id");
            $brands_id = Util::getParamNotEmpty("brands_id");


            //删除
            $obj = MallBrandsMaterial::model()->find("material_id=:material_id and  brands_id=:brands_id",
                array(":material_id" => $material_id,":brands_id" => $brands_id));

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }



}